package com.info.zhiduoduo.admin.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.AddressTaxRateFormBean;
import com.info.zhiduoduo.admin.model.formbean.crowdsource.SaveTaxRateFormBean;
import com.info.zhiduoduo.admin.model.formbean.invoice.ApprovalScreenshotFromBean;
import com.info.zhiduoduo.admin.model.formbean.invoice.EditRemarkFormBean;
import com.info.zhiduoduo.admin.model.formbean.maker.MakerListFormBean;
import com.info.zhiduoduo.admin.model.formbean.product.ProductListFormBean;
import com.info.zhiduoduo.admin.model.formbean.product.TakeProductFormBean;
import com.info.zhiduoduo.admin.model.formbean.taxer.*;
import com.info.zhiduoduo.common.configs.CommonConfig;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.invoice.InvoiceType;
import com.info.zhiduoduo.common.constants.maker.MakerType;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.excelconverter.ZddReadListener;
import com.info.zhiduoduo.common.exception.Asserts;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.*;
import com.info.zhiduoduo.core.service.*;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.core.service.wxmessage.WxTaxerWxMessageServiceImpl;
import com.info.zhiduoduo.repository.dto.maker.MakerDetailDto;
import com.info.zhiduoduo.repository.dto.maker.StudioBriefDto;
import com.info.zhiduoduo.repository.dto.maker.StudioDetailDto;
import com.info.zhiduoduo.repository.dto.product.OrderProductWxDto;
import com.info.zhiduoduo.repository.dto.taxer.TaxerCompanyListDto;
import com.info.zhiduoduo.repository.dto.taxer.TaxerInvoiceDto;
import com.info.zhiduoduo.repository.dto.taxer.TaxerInvoicesDto;
import com.info.zhiduoduo.repository.dto.taxer.TaxerPaymentDto;
import com.info.zhiduoduo.repository.entity.address.AddressTaxRate;
import com.info.zhiduoduo.repository.entity.company.CompanyAccount;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceInvoice;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProject;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProjectItem;
import com.info.zhiduoduo.repository.entity.invoice.Invoice;
import com.info.zhiduoduo.repository.entity.invoice.InvoiceInventory;
import com.info.zhiduoduo.repository.entity.maker.Maker;
import com.info.zhiduoduo.repository.entity.maker.MakerAccount;
import com.info.zhiduoduo.repository.entity.maker.MakerStudio;
import com.info.zhiduoduo.repository.entity.order.OrderProduct;
import com.info.zhiduoduo.repository.entity.recharge.RechargeRecord;
import com.info.zhiduoduo.repository.entity.system.BusinessManager;
import com.info.zhiduoduo.repository.entity.system.SystemManager;
import com.info.zhiduoduo.repository.entity.system.SystemProjectcategory;
import com.info.zhiduoduo.repository.entity.taxer.*;
import com.info.zhiduoduo.repository.mapper.maker.MakerAccountMapper;
import com.info.zhiduoduo.repository.mapper.maker.MakerMapper;
import com.info.zhiduoduo.repository.mapper.maker.MakerStudioMapper;
import com.info.zhiduoduo.repository.mapper.order.OrderProductMapper;
import com.info.zhiduoduo.repository.mapper.order.ProductMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerCompanyMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInviteCodeMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInvoiceMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerPaymentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ProductService extends BaseService {

    @Resource
    ProductMapper productMapper;
    @Resource
    OrderProductMapper orderProductMapper;

    @Autowired
    CommonConfig commonConfig;

    @Autowired
    WxMessageService wxMessageService;

    /**
     * 产品订单接口
     * admin端做个订单列表（展示订单时间、金额、手机号，状态）其中状态包含：待确认/已确认，支付完成的订单需要客户在后台点确认的，所以再加一个确认接口
     * 然后移动端也加个接口返回订单信息，包括上面这2个日期
     *
     * @param formBean
     * @return
     */
    public SmartResult listOrder(BaseSimpleFormBean<ProductListFormBean> formBean) {
        ProductListFormBean reqdata = formBean.getReqdata();
        String phoneNo = reqdata.getPhoneNo();
        String takeState = reqdata.getTakeState();
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        orderProductMapper.listOrderAll(phoneNo, takeState);
        return smartResultPage(page);
    }

    /**
     * 订单确认接口
     *
     * @param formBean
     * @return
     */
    public SmartResult take(BaseSimpleFormBean<TakeProductFormBean> formBean) {
        TakeProductFormBean reqdata = formBean.getReqdata();
        OrderProduct oneOrderProduct = orderProductMapper.getOneOrderProduct(reqdata.getOrderno());
        oneOrderProduct.setTakeState(reqdata.getTakeState().toString());
        oneOrderProduct.setTakeTime(DateUtil.nowTime());
        oneOrderProduct.setEffTime(reqdata.getEffTime());
        oneOrderProduct.updateById();

        //微信通知客户 智小多已开通.
        wxMessageService.notifyTakeOrder(oneOrderProduct.getOrderid());

        return SmartResult.success();

    }
}
